package com.example.jsonspinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.*;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Spinner proSpinner;
    private Spinner citySpinner;
    private Spinner countySpinner;
    static int provincePosition = 3;

    private String[] provinceDatas;  //省
    private String[] cityDatas;     //城市
    private String[] areaDatas;    //地区

    //列表选择的省市区
    private String selectedPro = "";
    private String selectedCity = "";
    private String selectedArea = "";

    //适配器
    private ArrayAdapter<String> proAdapter;
    private ArrayAdapter<String> cityAdapter;
    private ArrayAdapter<String> areaAdapter;

    //存储省对应的所以市
    private Map<String, String[]> cityDataMap = new HashMap<String, String[]>();
    //存储市对应的所有区
    private Map<String, String[]> areaMap = new HashMap<String, String[]>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        proSpinner = findViewById(R.id.spin_province);
        citySpinner = findViewById(R.id.spin_city);
        countySpinner = findViewById(R.id.spin_county);
        useSpinner1();

    }
    private void useSpinner1() {
        try {
            InputStream inputStream = null;

            inputStream = getResources().getAssets().open("data.txt");

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuffer.append(line);
                line = bufferedReader.readLine();
            }

            //存放省 城市  区
            JSONArray proArray = new JSONArray(stringBuffer.toString());
            //获取省份的数量
            provinceDatas = new String[proArray.length()];
            String provinceStr = null;

            for(int i=0;i<proArray.length();i++) {
                //解析省份  取出
                JSONObject proObject = (JSONObject) proArray.get(i);
                //拿到所有的省
                provinceStr = proObject.getString("name");
                provinceDatas[i] = provinceStr;


                String ctiyStr = null;
                //获取城市
                JSONArray cityArray = proObject.getJSONArray("city");
                cityDatas = new String[cityArray.length()];

                for (int j=0;j<cityArray.length();j++){
                    //解析城市  取出·
                    JSONObject cityObject = (JSONObject) cityArray.get(j);
                    //拿到所有城市
                   // String cityName = cityObject.getString("name");
                    ctiyStr = cityObject.getString("name");
                    cityDatas[j] = ctiyStr;


                    //地区    ??
                    JSONArray countyArry = cityObject.getJSONArray("area");
                    areaDatas = new String[countyArry.length()];

                    for (int k=0;k<countyArry.length();k++){
                        areaDatas[k] = countyArry.getString(k);

                    }
                    areaMap.put(ctiyStr, areaDatas);

                }
                // 存储省份对应的所有市
                cityDataMap.put(provinceStr, cityDatas);
            }


            //Spinner 配置
            proAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, provinceDatas);
          proAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            proSpinner.setAdapter(proAdapter);

            proSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    selectedPro = "";
                    selectedPro = (String) parent.getSelectedItem();
                    // 根据省份更新城市区域信息
                    updateCity(selectedPro);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });

            // 市选择
            citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    selectedCity = "";
                    selectedCity = (String) parent.getSelectedItem();
                    updateArea(selectedCity);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });

            // 区选择
            countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    selectedArea = "";
                    selectedArea = (String) parent.getSelectedItem();
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });

        } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void updateArea(String city) {
        String[] areas = areaMap.get(city);
        // 存在区
        if (areas != null) {
            // 存在区
            countySpinner.setVisibility(View.VISIBLE);
            areaAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item
                    , areas);
            areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            countySpinner.setAdapter(areaAdapter);
            areaAdapter.notifyDataSetChanged();
            countySpinner.setSelection(0);
        } else {
            countySpinner.setVisibility(View.GONE);
        }
    }

    private void updateCity(String pro) {
        String[] cities = cityDataMap.get(pro);
        for (int i = 0; i < cities.length; i++) {
            // 存在区
            cityAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item
                    , cities);
            cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            citySpinner.setAdapter(cityAdapter);
            cityAdapter.notifyDataSetChanged();
            citySpinner.setSelection(0);
        }
    }
}